<template>
  <div class="app-container Blog">
      <hz-search title="文章标签" :actionData="searchActionData" :havuSearch='false'></hz-search>
     <hz-table
        :label-data="tableData.cloumn"
        :table-data="tableData.data"
        :counts="count"
        pagi
        :load="loading"
        @selection-change="getSelectionChange"
        :current-page="this.searchParams.page"
        @pageChange="getCurrentPage"
      >
        <el-table-column prop="label" label="标签描述" > 
          <template slot-scope="scope">
            <span class="desc">{{scope.row.desc}}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" >
          <template slot-scope="scope">
            {{time(scope.row.createTime)}}
          </template>
        </el-table-column>
      
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <span class="tb-edit" @click="updateRow(scope.row)" >编辑</span>
            <span class="tb-delete" @click="deleteRow(scope.row)">移除</span>
          </template>
        </el-table-column>
    </hz-table>
    <el-dialog :title="dialogType === 'update' ? '编辑标签' : '创建标签'" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="form"  ref="ruleForm"  :rules="rules" label-width="120px" size='mini' style="width:400px">
        <el-form-item label="标签名称"  prop="label">
          <el-input v-model="form.label"  autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="标签描述"  prop="desc">
          <el-input v-model="form.desc" type="textarea" :autosize="{ minRows: 5}"  autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">{{dialogType === 'update' ? '保存' : '立即创建'}}</el-button>
          <el-button @click="dialogFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { time } from '@/utils/tool'
import { postAddTag, postUpdateTag, getTagList, postDeleteTag, getTagById } from '@/api/modules/blog'
import HzSearch from '../../../components/compon/HzSearch.vue'
import HzTable from '../../../components/compon/HzTable.vue'
export default {
  components: { HzSearch, HzTable },
  name: 'Blog',
  data () {
    return {
      searchParams: {
        page: 1,
        rows: 5
      },
      loading: false,
      count: 0,
      tableData: {
        cloumn: [
          { label: '标签名称', prop: 'label' },
        ],
        data: []
      },
      dialogType: '',
      dialogFormVisible: false,
      form: {
        value: '',
        label: '',
        desc:''
      },
      rules: {
        label: [
          { required: true, message: '请输入标签名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入标签描述', trigger: 'blur' }
        ]
      },
      //  搜索栏
      searchActionData: [{
        tip: '添加标签', icon: 'el-icon-circle-plus-outline', handleClick: this.handleAdd, auth: () => { return true }
      }
      ]
    }
  },
  computed: {
    time () {
      return time
    }
  },
  created () {
    this.fetch()
  },
  methods: {
    // 获取列表
    async fetch () {
      this.loading = true
      const res = await getTagList(this.searchParams)
      this.tableData.data = res.rows
      this.count = res.total
      this.loading = false
    },
    // 删除标签
    deleteRow (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await postDeleteTag({ id: row._id })
        this.msgSuccess('删除成功!')
        this.fetch()
      })
    },
    async updateRow (row) {
      this.form.id = row._id
      const res = await getTagById({ id: row._id })
      Object.assign(this.form, res)
      this.dialogType = 'update'
      this.dialogFormVisible = true
    },
    // 新增标签
    handleAdd () {
      this.resetForm()
      this.dialogType = 'add'
      this.dialogFormVisible = true
    },
    // 提交新增/编辑
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (!valid) {
          console.log('error submit!!')
          return false
        } else {
          this.form.value = this.form.label
          if (this.dialogType === 'update') {
            await postUpdateTag(this.form).then(res => {
              this.msgSuccess('操作成功!')
              this.fetch()
              this.dialogFormVisible = false
            })
          } else {
            postAddTag(this.form).then((result) => {
              this.msgSuccess('添加成功!')
              this.fetch()
              this.dialogFormVisible = false
            })
          }
        }
      })
    },
    resetForm () {
      this.form = {
        value:'',
        label:'',
        desc:''
      }
    },
    // 下拉框选择每页显示条数
    getSelectionChange (val) {
      this.searchParams.rows = val
      this.searchParams.page = 1
      this.fetch()
    },
    // 切换分页
    getCurrentPage (val) {
      this.searchParams.page = val
      this.fetch()
    }
  }
}
</script>

<style  lang="scss" scoped>
.desc{
  overflow: hidden;text-overflow: ellipsis;white-space: nowrap;
}
</style>
